<?php
namespace app\admin\controller;
use app\admin\model\Jiangli as Jlmodel;

class Jiangli extends Base
{
    /**
     * 奖励列表
     * @return \think\response\View
     */
    public function index()
    {
        $jl = new Jlmodel();
        $list = $jl->where('enable',0)->order('is_select','desc')->select();
		$this->assign('httpurl',config("httpurl"));
        $this->assign('list',$list);
        return view();
    }

    /**
     * 添加奖励语
     */
    public function addInfo()
    {
		$type = I('param.type');
        $data = input();
		if($type==2){
		$files = request()->file('img');
        $info = $files->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
        if($info) {
			$item['type']=2;
            $path = $info->getSaveName();
            $pathinfo = DS . 'static' . DS .'uploads' . DS . $path;
            $item['content'] = str_replace('\\','/',$pathinfo);
        }
		}else{
			$item['content'] = $data['ncontent'];
		}
		
        $jl = new Jlmodel();
        
        //首先判断是否存在奖励语，如果不存在，第一条即为首选
        $rel = $jl->where('enable',0)->select();
        if($rel) {
            //证明已有
            $jl->insert($item);
            $this->redirect('Jiangli/index');
        }else {
            //证明没有
            $item['is_select'] = 1;
            $jl->insert($item);
            $this->redirect('Jiangli/index');
        }
    }

    /**
     * 删除奖励语
     */
    public function delInfo()
    {
        $data = input();
        $jl = new Jlmodel();
        $list = $jl->where('id',$data['id'])->update(['enable'=>1]);
        if($list) {
            $this->redirect('Jiangli/index');
        }
    }

    /**
     * 奖励语置顶
     */
    public function setTop()
    {
        $data = input();
        //清除置顶项，将当前选项置顶
        $jl = new Jlmodel();
        $jl->where('enable',0)->where('is_select',1)->update(['is_select'=>0]);
        $list = $jl->where('id',$data['id'])->update(['is_select'=>1]);
        if($list) {
            $this->redirect('Jiangli/index');
        }
    }
}
